package com.jike.noobmoney.service;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.jike.noobmoney.mvp.view.activity.LaunchActivity;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by kekex on 2018/11/24.
 * 自定义消息
 */

public class JpushSelfServie extends BroadcastReceiver {
    private static final String TAG = "JpushSelfServie";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        Log.e(TAG, "onReceive - " + intent.getAction() + ", extras: " + bundle.toString());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            Log.e(TAG, "JPush 用户注册成功");
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "接受到推送下来的自定义消息");
            processCustomMessage(context, bundle);
        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            Log.e(TAG, "接受到推送下来的通知");
        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            Log.e(TAG, "用户点击打开了通知");
            opneActivity(context);
        } else {
            Log.e(TAG, "Unhandled intent - " + intent.getAction());
        }
    }


    /**
     * 用户点击启动app
     * */
    private void opneActivity(Context context) {
        context.startActivity(new Intent(context, LaunchActivity.class));
    }

    private void processCustomMessage(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);

        Log.e("xuke", "title=" + title + ",message=" + message);
    }


}
